{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}

<script>

$(function() {
    var id = '{{ id }}';
    if (id) {
        getJSON('/api/categories/' + id, function (err, category) {
            if (err) {
                return fatal(err);
            }
            initVM(category);
        });
    }
    else {
        initVM({
            id: ''
        });
    }
});

function initVM(category) {
    var vm = new Vue({
        el: '#vm',
        data: {
            name: category.name,
            tag: category.tag,
            description: category.description
        },
        methods: {
            submit: function(event) {
                event.preventDefault();
                $('#vm').find('form').postJSON('{{ form.action }}', this.$data, function(err, result) {
                    if (err) {
                        return;
                    }
                    location.assign('{{ form.redirect }}');
                });
            }
        }
    });
    $('#loading').hide();
    $('#vm').show();
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> Loading...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-tab" data-uk-tab>
            <li><a href="#0" onclick="location.assign('/manage/article/')">{{ _('Articles') }}</a></li>
            <li class="uk-active"><a href="#0">{{ _('Categories') }}</a></li>
        </ul>
        <form v-on="submit: submit" class="uk-margin uk-form uk-form-stacked">
            <legend>{{ form.name }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">Name:</label>
                    <div class="uk-form-controls">
                        <input v-model="name" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">Tag:</label>
                    <div class="uk-form-controls">
                        <input v-model="tag" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">Description:</label>
                    <div class="uk-form-controls">
                        <input v-model="description" type="text" maxlength="100" class="uk-width-1-1">
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="uk-form-controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> Save</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> Cancel</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
